From 8b2a622504859463ad4fc21bdc395d850b06fb28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Sat, 8 Nov 2025 15:24:13 +0100 Subject: [PATCH] statefiles: add function to write IPv4 hosts MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Analogous to statefiles_write_host6(). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- src/statefiles.c | 58 ++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/statefiles.c b/src/statefiles.c index f090c05..b2e754e 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -41,26 +41,40 @@ struct write_ctxt { int buf_idx; }; +static void statefiles_write_host(const char *ipbuf, const char *hostname, struct write_ctxt *ctxt) +{ + char exp_dn[DNS_MAX_NAME_LEN]; + + if (dn_expand(ctxt->iface->search, ctxt->iface->search + ctxt->iface->search_len, + ctxt->iface->search, exp_dn, sizeof(exp_dn)) > 0) + fprintf(ctxt->fp, "%s\t%s.%s\t%s\n", ipbuf, hostname, exp_dn, hostname); + else + fprintf(ctxt->fp, "%s\t%s\n", ipbuf, hostname); +} + static void statefiles_write_host6(struct dhcpv6_lease *lease, struct in6_addr *addr, int prefix, _unused uint32_t pref_lt, _unused uint32_t valid_lt, void *arg) { struct write_ctxt *ctxt = (struct write_ctxt *)arg; char ipbuf[INET6_ADDRSTRLEN]; - char exp_dn[DNS_MAX_NAME_LEN]; - if (!(lease->flags & OAF_DHCPV6_NA)) + if (!lease->hostname || lease->flags & OAF_BROKEN_HOSTNAME || !(lease->flags & OAF_DHCPV6_NA)) return; + inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf)); + statefiles_write_host(ipbuf, lease->hostname, ctxt); +} + +static void statefiles_write_host4(struct write_ctxt *ctxt, struct dhcpv4_lease *lease) +{ + char ipbuf[INET_ADDRSTRLEN]; + struct in_addr addr = { .s_addr = lease->addr }; + if (!lease->hostname || lease->flags & OAF_BROKEN_HOSTNAME) return; - inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf)); - - if (dn_expand(ctxt->iface->search, ctxt->iface->search + ctxt->iface->search_len, - ctxt->iface->search, exp_dn, sizeof(exp_dn)) > 0) - fprintf(ctxt->fp, "%s\t%s.%s\t%s\n", ipbuf, lease->hostname, exp_dn, lease->hostname); - else - fprintf(ctxt->fp, "%s\t%s\n", ipbuf, lease->hostname); + inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf)); + statefiles_write_host(ipbuf, lease->hostname, ctxt); } static void statefiles_write_state6(struct dhcpv6_lease *lease, struct in6_addr *addr, int prefix, @@ -129,9 +143,11 @@ static void statefiles_write_hosts(time_t now) if (!(lease->flags & OAF_BOUND)) continue; - if (INFINITE_VALID(lease->valid_until) || lease->valid_until > now) - odhcpd_enum_addr6(ctxt.iface, lease, now, - statefiles_write_host6, &ctxt); + if (!INFINITE_VALID(lease->valid_until) && lease->valid_until <= now) + continue; + + odhcpd_enum_addr6(ctxt.iface, lease, now, + statefiles_write_host6, &ctxt); } } @@ -142,22 +158,10 @@ static void statefiles_write_hosts(time_t now) if (!(lease->flags & OAF_BOUND)) continue; - char ipbuf[INET_ADDRSTRLEN]; - struct in_addr addr = { .s_addr = lease->addr }; - inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1); - - if (lease->hostname && !(lease->flags & OAF_BROKEN_HOSTNAME)) { - fputs(ipbuf, ctxt.fp); - - char b[256]; - - if (dn_expand(ctxt.iface->search, - ctxt.iface->search + ctxt.iface->search_len, - ctxt.iface->search, b, sizeof(b)) > 0) - fprintf(ctxt.fp, "\t%s.%s", lease->hostname, b); + if (!INFINITE_VALID(lease->valid_until) && lease->valid_until <= now) + continue; - fprintf(ctxt.fp, "\t%s\n", lease->hostname); - } + statefiles_write_host4(&ctxt, lease); } } } -- 2.30.2